package com.sanpu.kxe.business.member.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.member.adapter.MemberTeamAdapter;
import com.sanpu.kxe.business.member.bean.MemberTeamNumberOutput;
import com.sanpu.kxe.business.member.bean.MemberTeamOutput;
import com.sanpu.kxe.business.member.contract.MemberConfig;
import com.sanpu.kxe.business.member.contract.MemberTeamListContract;
import com.sanpu.kxe.business.member.presenter.MemberTeamListPresenter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;
import com.sanpu.kxe.utils.IntentTool;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：团队明细
 * Created by 薛勇军 on 2019/9/26.
 */

public class MemberTeamListActivity extends BaseTitleActivity implements MemberTeamListContract.View {

    @Bind(R.id.totalRtv)
    RoundTextView totalRtv;
    @Bind(R.id.verifiedRtv)
    RoundTextView verifiedRtv;
    @Bind(R.id.unverifiedRtv)
    RoundTextView unverifiedRtv;

    @Bind(R.id.totalTv)
    TextView totalTv;
    @Bind(R.id.verifiedTv)
    TextView verifiedTv;
    @Bind(R.id.unverifiedTv)
    TextView unverifiedTv;

    @Bind(R.id.recyclerview)
    RecyclerView vRecyclerview;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.emptyView)
    View emptyView;

    private MemberTeamAdapter vTeamAdapter;

    private MemberTeamListContract.Presenter mPresenter;

    private int nowPage = 0;

    private String queryType = MemberConfig.QUERYTYPE_ALL, merchantCode;

    private boolean mIsDirect = false;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_member_team_list;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("团队明细");

        if (getIntent().hasExtra(MemberConfig.INTENT_MERCHANTCODE)) {
            mIsDirect = false;
            merchantCode = getIntent().getStringExtra(MemberConfig.INTENT_MERCHANTCODE);
        } else {
            mIsDirect = true;
            UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
            if (centerOutput != null) {
                merchantCode = centerOutput.getMerchantCode();
            }
        }

        vRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        vTeamAdapter = new MemberTeamAdapter(mIsDirect);
        vRecyclerview.setAdapter(vTeamAdapter);
        vTeamAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);

        mPresenter = new MemberTeamListPresenter(this);
        mPresenter.myTeamNumber(merchantCode);
        mPresenter.myTeamList(nowPage, queryType, merchantCode);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.myTeamList(nowPage, queryType, merchantCode);
                vTeamAdapter.setEnableLoadMore(true);
            }
        });
        vTeamAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.callTelLl) {
                    MemberTeamOutput teamOutput = (MemberTeamOutput) adapter.getItem(position);
                    if (teamOutput != null && !TextUtils.isEmpty(teamOutput.getPhoneNumber())) {
                        Uri uri = Uri.parse("tel:" + teamOutput.getPhoneNumber());
                        Intent it = new Intent(Intent.ACTION_DIAL, uri);
                        startActivity(it);
                    }
                }
            }
        });
        vTeamAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mIsDirect) {
                    MemberTeamOutput teamOutput = (MemberTeamOutput) adapter.getItem(position);
                    if (teamOutput != null && !TextUtils.isEmpty(teamOutput.getMerchantCode())) {
                        Intent intent = new Intent(mContext, MemberTeamListActivity.class);
                        intent.putExtra(MemberConfig.INTENT_MERCHANTCODE, teamOutput.getMerchantCode());
                        IntentTool.startActivity(mContext, intent);
                    }
                }
            }
        });
        vTeamAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.myTeamList(nowPage, queryType, merchantCode);
            }
        }, vRecyclerview);
        vTeamAdapter.setPreLoadNumber(2);
    }

    @OnClick({R.id.totalRtv, R.id.verifiedRtv, R.id.unverifiedRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.totalRtv:
                initHeadGroupView();
                totalRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_base));
                totalRtv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                if (!TextUtils.equals(MemberConfig.QUERYTYPE_ALL, queryType)) {
                    queryType = MemberConfig.QUERYTYPE_ALL;
                    nowPage = 0;
                    mPresenter.myTeamList(nowPage, queryType, merchantCode);
                }
                break;
            case R.id.verifiedRtv:
                initHeadGroupView();
                verifiedRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_base));
                verifiedRtv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                if (!TextUtils.equals(MemberConfig.QUERYTYPE_VERIFIED, queryType)) {
                    queryType = MemberConfig.QUERYTYPE_VERIFIED;
                    nowPage = 0;
                    mPresenter.myTeamList(nowPage, queryType, merchantCode);
                }
                break;
            case R.id.unverifiedRtv:
                initHeadGroupView();
                unverifiedRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_base));
                unverifiedRtv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                if (!TextUtils.equals(MemberConfig.QUERYTYPE_UNVERIFIED, queryType)) {
                    queryType = MemberConfig.QUERYTYPE_UNVERIFIED;
                    nowPage = 0;
                    mPresenter.myTeamList(nowPage, queryType, merchantCode);
                }
                break;
        }
    }

    @Override
    public void showTeamNumberView(MemberTeamNumberOutput numberOutput) {
        if (numberOutput != null) {
            if (!TextUtils.isEmpty(numberOutput.getAllNumber())) {
                totalRtv.setText("全部(" + numberOutput.getAllNumber() + ")");
                totalTv.setText("团队总数：" + numberOutput.getAllNumber() + "人");
            }
            if (!TextUtils.isEmpty(numberOutput.getRealNameNumber())) {
                verifiedRtv.setText("已认证(" + numberOutput.getRealNameNumber() + ")");
                verifiedTv.setText("认证总数：" + numberOutput.getRealNameNumber() + "人");
            }
            if (!TextUtils.isEmpty(numberOutput.getUnRealNameNumber())) {
                unverifiedRtv.setText("未认证(" + numberOutput.getUnRealNameNumber() + ")");
                unverifiedTv.setText("直推人数：" + numberOutput.getUnRealNameNumber() + "人");
            }
        }
    }

    @Override
    public void showTeamListView(List<MemberTeamOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size()>0) {
            emptyView.setVisibility(View.GONE);
            vSwipeRefreshLayout.setVisibility(View.VISIBLE);
            if (isFirst) {
                vTeamAdapter.setNewData(outputs);
            } else {
                vTeamAdapter.addData(outputs);
            }
            nowPage++;
        } else if (isFirst) {
            emptyView.setVisibility(View.VISIBLE);
            vSwipeRefreshLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vTeamAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vTeamAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vTeamAdapter.loadMoreFail();
                break;
        }
    }

    /**
     * 初始化头部tab
     */
    private void initHeadGroupView() {
        totalRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
        verifiedRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
        unverifiedRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));

        totalRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
        verifiedRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
        unverifiedRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MemberTeamListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
